package day2;

import java.util.Scanner;

public class demo3 {
    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        int n=scanner.nextInt();
        String str1=scanner.next();
        String str2=scanner.next();

        String []strs=new String[n];
        for (int i = 0; i <n ; i++) {
            strs[i]=scanner.next();
        }
int ret=Integer.MAX_VALUE;
        int x1=-1,x2=-1;
        for (int i = 0; i <n ; i++) {
            if(str1.equals(strs[i])){
                x1=i;
                if (x2!=-1){
                    ret=Math.min(ret,Math.abs(x2-x1));
                }
            } else if (str2.equals(strs[i])) {
                x2=i;
                if (x1!=-1){
                    ret=Math.min(ret,Math.abs(x1-x2));
                }
            }
        }
        if (ret!=Integer.MAX_VALUE) System.out.println(ret);
        else System.out.println(-1);
    }
}
